package com.atfcm.stack;

import java.util.Deque;
import java.util.LinkedList;

public class T_150_逆波兰表达式求值 {
    // tokens = ["2","1","+","3","*"]
    public int evalRPN(String[] tokens) {
        Deque<Integer> stack = new LinkedList<>();
        for (String token : tokens) {
            switch (token) {
                case "+":
                    stack.push(stack.pop() + stack.pop());
                    break;

                case "-":
                        stack.push(-(stack.pop() - stack.pop()));
                        break;
                 case "*":
                         stack.push(stack.pop() * stack.pop());
                         break;
                 case "/":
                     Integer pop = stack.pop();
                     Integer pop1 = stack.pop();
                     stack.push( pop1/pop );
                         break;
                 default:
                             stack.push(Integer.parseInt(token));
            }

        }
        return stack.pop();
    }
}
